"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _system = require("../../../static/utils/system.js");

var _system2 = _interopRequireDefault(_system);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {
    default: obj
  };
}

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}

var app = getApp();
var WxParse = require("../../../wxParse/wxParse.js");
exports.default = Page({
  data: {
    isShowModal: false,
    isAuthorize: false,
    animation: "",
    phoneList: [],
    phone: "",
    payType: ""
  },
  onLoad: function onLoad(e) {
    console.log(wx.getStorageSync("info"))
    var _this = this;
    var userType = wx.getStorageSync("info").type || ""; {
      var host = app.globalData.host;
      var openid = _this.data.openid || wx.getStorageSync("openid") || "";
      var phone = wx.getStorageSync('phone');
      var phoneinfo = wx.getStorageSync('phoneinfo');
      if (!phone) {
        var _this = this
        var host = "https://api.bjlya.com.cn/xcx/app.php?";
        var data = {
          mod: "do",
          act: "tips",
          ssid: wx.getStorageSync('openid') || ''
        };
        wx.request({
          url: host,
          data: data,
          method: "GET",
          header: {
            "content-type": "application/json"
          },
          success: function success(result) {
            // console.log(result)
            var info = result.data.info
            _this.setData({
              baseinfo: info.baseinfo
            })
          }
        })
      }
      var data = {
        mod: "product",
        act: "show",
        ssid: openid,
        mid: e.id || e.mid
      };
      wx.request({
        url: host,
        data: data,
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          _this.setData({
            mid: e.id || e.mid,
            openid: openid,
            userType: userType * 1,
            proDetail: res.data.info
          });
          wx.setStorageSync('cid', e.userid);
          WxParse.wxParse("article", "html", res.data.info.content, _this, 5);
        }
      });
    }
    //实验开始
    if (!openid) {
      var _this = this;
      _this.setData({
        isAuthorize: true
      });
    } else if (openid && !phone) {
      var _this = this;
      _this.setData({
        phoneinfo: phoneinfo,
        isPhone: true
      })
    }

  //实验结束


  },
  onReady: function onReady() {},
  onHide: function onHide() {
    this.setData({
      isAuthorize: false
    });
  },
  onShareAppMessage: function onShareAppMessage(res) {
    var _this = this;
    var mid = _this.data.mid;
    var info = wx.getStorageSync("info");
    if (info) {
      var userid = info.id;
      console.log(userid)
      return {
        title: "门品学社",
        desc: "当下最具人气的小程序！",
        path: "/pages/offline/offline?url=/pages/offline/detail/detail&mid=" + mid + "&userid=" + userid
      };
    } else {
      return {
        title: "门品学社",
        desc: "当下最具人气的小程序！",
        path: "/pages/offline/offline?url=/pages/offline/detail/detail&mid=" + mid
      };
    }
  },
  getOpenId: function getOpenId() {
    var _this2 = this;

    this.setData({
      openid: wx.getStorageSync("openid")
    }, function() {
      var _this = _this2;
      var openid = _this.data.openid;
      if (openid) {
        var host = app.globalData.host;
        var data = {
          mod: "product",
          act: "show",
          ssid: openid,
          mid: _this.data.mid
        };
        wx.request({
          url: host,
          data: data,
          method: "post",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function success(res) {
            _this.setData({
              proDetail: res.data.info
            });
          }
        });
      }
    });
  },
  setPayType: function setPayType(e) {
    var _this = this;
    var payType = e.currentTarget.dataset.index * 1 + 1;
    var price = e.currentTarget.dataset.payprice;
    var integral = e.currentTarget.dataset.payintegral;
    var payTheWay = e.currentTarget.dataset.paytype;
    _this.setData({
      price: price,
      integral: integral,
      payType: payType,
      payTheWay: payTheWay
    });
  },
  delPhone: function delPhone(e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var phoneList = _this.data.phoneList;
    phoneList.splice(index, 1);
    _this.setData({
      phoneList: phoneList
    }, function() {
      var length = _this.data.phoneList.length;
      wx.request({
        url: app.globalData.host,
        data: {
          mod: "product",
          act: "change",
          ssid: _this.data.openid,
          num: length,
          mid: _this.data.mid
        },
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          _this.setData(_defineProperty({}, "proDetail.payway", res.data.info));
        }
      });
    });
  },
  addPhone: function addPhone() {
    var _this = this;
    var regex = /^.{11}$/;
    var phone = _this.data.phone;
    var length = _this.data.phoneList.length;
    if (regex.test(phone)) {
      if (length < 3) {
        var _this$setData2;

        _this.setData((_this$setData2 = {}, _defineProperty(_this$setData2, "phoneList[" + length + "]", _this.data.phone), _defineProperty(_this$setData2, "phone", ""), _this$setData2), function() {
          var length = _this.data.phoneList.length;
          wx.request({
            url: app.globalData.host,
            data: {
              mod: "product",
              act: "change",
              ssid: _this.data.openid,
              num: length,
              mid: _this.data.mid
            },
            method: "post",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function success(res) {
              _this.setData(_defineProperty({}, "proDetail.payway", res.data.info));
            }
          });
        });
      } else {
        wx.showToast({
          title: "最多只能赠送给3人哦！",
          icon: "none",
          duration: 1000
        });
      }
    } else {
      wx.showToast({
        title: "手机号码只能为11位数字！",
        icon: "none",
        duration: 1000
      });
    }
  },
  regex: function regex(e) {
    var _this = this;
    var value = e.detail.value.replace(/\./g, "");
    _this.setData({
      phone: value
    });
  },
  refuse: function refuse() {
    var _this = this;
    _this.setData({
      isAuthorize: false
    });
    // wx.switchTab({
    //   url: "/pages/online/online"
    // });
  },
  login: function login(info) {
    var _this = this;
    var iv = info.detail.iv;
    var userInfo = info.detail.userInfo;
    var encryptedData = info.detail.encryptedData;
    if (userInfo) {
      wx.login({
        success: function success(response) {
          var code = response.code;
          var host = "https://api.bjlya.com.cn/xcx/app.php?";
          var data = {
            mod: "do",
            act: "openid",
            mid: code,
            iv: iv,
            encryptedData: encryptedData,
            cid: wx.getStorageSync("cid") || ""
          };
          wx.request({
            url: host,
            data: data,
            method: "GET",
            header: {
              "content-type": "application/json"
            },
            success: function success(result) {
              wx.setStorageSync("type", result.data.info.type);
              wx.setStorageSync("openid", result.data.info.ssid);
              wx.setStorageSync("session_key", result.data.info.session);
              _this.setData({
                isAuthorize: false
              });
              var method = "post";
              var host = "https://api.bjlya.com.cn/xcx/app.php?";
              var data = {
                mod: "do",
                act: "info",
                ssid: result.data.info.ssid
              };
              if (method == "post") {
                wx.request({
                  url: host,
                  data: data,
                  method: method,
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function success(res) {
                    wx.setStorageSync("info", res.data.info);
                  }
                });
              } else if (method == "get") {
                wx.request({
                  url: host,
                  data: data,
                  method: method,
                  header: {
                    "Content-Type": "application/json"
                  },
                  success: function success(res) {
                    wx.setStorageSync("info", res.data.info);
                  }
                });
              }
            }
          });
        },
        fail: function fail(res) {
          // wx.switchTab({
          //   url: "/pages/offline/offline"
          // });
        }
      });
    } else {
      console.log('最后不同意，则返回')
       wx.switchTab({
         url: "/pages/offline/offline"
       });
    }
    wx.showToast({
      icon: "loading",
      duration: 1000,
      mask: false,
      success: function success() {
        setTimeout(function() {
          _this.setData({
            openid: wx.getStorageSync("openid"),
            info: wx.getStorageSync("info"),
            isAuthorize: false
          });
        }, 1000);
      }
    });
  },
  linkPayService: function linkPayService(e) {
    var prop = e.currentTarget.dataset.type;
    var _this = this;
    var openid = wx.getStorageSync("openid") || "";
    if (!openid) {
      _this.setData({
        isAuthorize: true
      });
    } else {
      wx.navigateTo({
        url: "payService/payService?type=" + prop
      });
    }
  },
  showModal: function showModal(e) {
    var _this = this;
    var mode = e.currentTarget.dataset.mode;
    var openid = wx.getStorageSync("openid") || "";
    if (!openid) {
      _this.setData({
        isAuthorize: true
      });
    } else {
      _this.setData({
        mode: mode,
        isShowModal: true
      });
    }
  },
  hideModal: function hideModal() {
    var _this = this;
    _this.setData({
      payType: "",
      isShowModal: false
    });
  },
  in: function _in() {
    // console.log('阻止冒泡');
  },
  ask: function ask(e) {
    var _this = this;
    var mobile = e.detail.value.phone;
    if (mobile) {
      wx.request({
        url: app.globalData.host,
        data: {
          mod: "product",
          act: "claim",
          ssid: _this.data.openid,
          mid: _this.data.mid,
          price: _this.data.price,
          mobile: mobile,
          formid: e.detail.formId
        },
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          wx.showToast({
            title: res.data.txt,
            icon: "none",
            duration: 1000,
            mask: false,
            success: function success() {
              setTimeout(function() {
                _this.setData({
                  phone: "",
                  isShowModal: false
                });
              }, 1000);
            }
          });
        }
      });
    } else {
      wx.showToast({
        title: "请添加索取人号码！",
        icon: "none",
        duration: 1000,
        mask: false
      });
    }
  },
  gift: function gift(e) {
    var _this = this;
    var payType = _this.data.payType;
    var payTheWay = _this.data.payTheWay;
    var length = _this.data.phoneList.length;
    if (length > 0) {
      if (payType) {
        wx.request({
          url: app.globalData.host,
          data: {
            mod: "product",
            act: "give",
            ssid: _this.data.openid,
            mid: _this.data.mid,
            price: _this.data.price,
            integral: _this.data.integral,
            mobile: _this.data.phoneList.join(","),
            oid: "",
            payway: _this.data.payTheWay,
            formid: e.detail.formId
          },
          method: "post",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function success(res) {
            var ret = res.data.ret;
            if (payTheWay != "integral") {
              if (ret != -1) {
                var oid = res.data.info.oid;
                var amount = res.data.info.amount;
                var openid = _this.data.openid;
                if (oid > 0) {
                  wx.request({
                    url: app.globalData.WxHost + "ssid=" + openid + "&mid=" + oid + "&money=" + amount,
                    success: function success(response) {
                      wx.requestPayment({
                        appId: response.data.appId,
                        nonceStr: response.data.nonceStr,
                        timeStamp: response.data.timeStamp,
                        package: response.data.package,
                        signType: response.data.signType,
                        paySign: response.data.paySign,
                        success: function success(result) {
                          _this.setData({
                            phone: "",
                            payType: "",
                            phoneList: [],
                            isShowModal: false
                          }, function() {
                            wx.showToast({
                              title: "支付成功！",
                              icon: "none",
                              duration: 1000,
                              mask: false,
                              success: function success() {
                                setTimeout(function() {
                                  var length = _this.data.phoneList.length;
                                  wx.request({
                                    url: app.globalData.host,
                                    data: {
                                      mod: "product",
                                      act: "change",
                                      ssid: _this.data.openid,
                                      num: length,
                                      mid: _this.data.mid
                                    },
                                    method: "post",
                                    header: {
                                      "Content-Type": "application/x-www-form-urlencoded"
                                    },
                                    success: function success(res) {
                                      _this.setData(_defineProperty({}, "proDetail.payway", res.data.info));
                                    }
                                  });
                                }, 1000);
                              }
                            });
                          });
                        },
                        fail: function fail(result) {
                          wx.showToast({
                            title: "支付失败！",
                            icon: "none",
                            duration: 1000,
                            mask: false
                          });
                        }
                      });
                    }
                  });
                } else {
                  wx.showToast({
                    title: res.data.txt,
                    icon: "none",
                    duration: 1000,
                    mask: false,
                    success: function success() {
                      setTimeout(function() {
                        _this.setData({
                          phone: "",
                          payType: "",
                          phoneList: [],
                          isShowModal: false
                        });
                      }, 1000);
                    }
                  });
                }
              } else {
                wx.showToast({
                  title: res.data.txt,
                  icon: "none",
                  duration: 1000,
                  mask: false
                });
              }
            } else {
              if (ret != -1) {
                wx.showToast({
                  title: res.data.txt,
                  icon: "none",
                  duration: 1000,
                  mask: false,
                  success: function success() {
                    setTimeout(function() {
                      _this.setData({
                        phone: "",
                        payType: "",
                        phoneList: [],
                        isShowModal: false
                      });
                    }, 1000);
                  }
                });
              } else {
                wx.showToast({
                  title: res.data.txt,
                  icon: "none",
                  duration: 1000,
                  mask: false
                });
              }
            }
          }
        });
      } else {
        wx.showToast({
          title: "请选择支付方式！",
          icon: "none",
          duration: 1000,
          mask: false
        });
      }
    } else {
      wx.showToast({
        title: "请添加至少一个号码！",
        icon: "none",
        duration: 1000,
        mask: false
      });
    }
  },
  buy: function buy(e) {
    var _this = this;
    var payType = _this.data.payType;
    var payTheWay = _this.data.payTheWay;
    if (payType) {
      wx.request({
        url: app.globalData.host,
        data: {
          mod: "product",
          act: "buy",
          ssid: _this.data.openid,
          mid: _this.data.mid,
          price: _this.data.price,
          integral: _this.data.integral,
          oid: "",
          payway: _this.data.payTheWay,
          formid: e.detail.formId
        },
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          var ret = res.data.ret;
          if (payTheWay != "integral") {
            if (ret != -1) {
              var oid = res.data.info.oid;
              var amount = res.data.info.amount;
              var openid = _this.data.openid;
              wx.request({
                url: app.globalData.WxHost + "ssid=" + openid + "&mid=" + oid + "&money=" + amount,
                success: function success(response) {
                  wx.requestPayment({
                    appId: response.data.appId,
                    nonceStr: response.data.nonceStr,
                    timeStamp: response.data.timeStamp,
                    package: response.data.package,
                    signType: response.data.signType,
                    paySign: response.data.paySign,
                    success: function success(result) {
                      _this.setData({
                        phone: "",
                        payType: "",
                        phoneList: [],
                        isShowModal: false
                      }, function() {
                        wx.showToast({
                          title: "支付成功！",
                          icon: "none",
                          duration: 1000,
                          mask: false,
                          success: function success() {
                            setTimeout(function() {
                              var length = _this.data.phoneList.length;
                              wx.request({
                                url: app.globalData.host,
                                data: {
                                  mod: "product",
                                  act: "change",
                                  ssid: _this.data.openid,
                                  num: length,
                                  mid: _this.data.mid
                                },
                                method: "post",
                                header: {
                                  "Content-Type": "application/x-www-form-urlencoded"
                                },
                                success: function success(res) {
                                  _this.setData(_defineProperty({}, "proDetail.payway", res.data.info));
                                }
                              });
                            }, 1000);
                          }
                        });
                      });
                    },
                    fail: function fail(result) {
                      wx.showToast({
                        title: "支付失败！",
                        icon: "none",
                        duration: 1000,
                        mask: false
                      });
                    }
                  });
                }
              });
            } else {
              wx.showToast({
                title: res.data.txt,
                icon: "none",
                duration: 1000,
                mask: false
              });
            }
          } else {
            if (ret != -1) {
              wx.showToast({
                title: res.data.txt,
                icon: "none",
                duration: 1000,
                mask: false,
                success: function success() {
                  setTimeout(function() {
                    _this.setData({
                      phone: "",
                      payType: "",
                      phoneList: [],
                      isShowModal: false
                    });
                  }, 1000);
                }
              });
            } else {
              wx.showToast({
                title: res.data.txt,
                icon: "none",
                duration: 1000,
                mask: false
              });
            }
          }
        }
      });
    } else {
      wx.showToast({
        title: "请选择支付方式！",
        icon: "none",
        duration: 1000,
        mask: false
      });
    }
  },
  free: function free(e) {
    var _this = this;
    wx.request({
      url: app.globalData.host,
      data: {
        mod: "product",
        act: "get",
        ssid: _this.data.openid,
        mid: _this.data.mid,
        formid: e.detail.formId
      },
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function success(res) {
        wx.showToast({
          title: res.data.txt,
          icon: "none",
          duration: 1000,
          mask: false,
          success: function success() {
            setTimeout(function() {
              _this.setData({
                isShowModal: false
              });
            }, 1000);
          }
        });
      }
    });
  },
  showLook: function() {
    var _this = this;
    _this.setData({
      look: true
    })
  },
  hideLook: function() {
    var _this = this;
    _this.setData({
      look: false
    })
  },
  eq: function() {}
});